<template>
  <div class="plugin-header">
    <div class="plugin-header__title" @click="emit('back')">
      <Icon type="md-arrow-round-back" size="20" />
      {{title}}
    </div>
    <div class="plugin-header__toolbar">
      <Space>
        <Input v-model="searchText" placeholder="请搜索" search @on-search="getList()"></Input>
        <Button type="primary" @click="addProject()" icon="md-add">新增</Button>
      </Space>
    </div>
  </div>
  <div class="grid-context">
    <div class="plugin-content" v-if="dataList.length" v-loading="loading">
      <div
        class="plugin-content-item"
        v-for="(item, i) in dataList"
        :key="i"
        @click="handleItemClick(item.id)"
      >
        <div class="header">
          <div class="left">
            <div class="title">{{ item.name }}</div>
            <div class="description">{{ item.description }}</div>
          </div>
          <div class="right">
            <Dropdown>
              <Icon type="ios-more" />
              <template #list>
                <DropdownMenu>
                  <DropdownItem @click.stop="handleRemove(item, i)">
                    <text-button style="width: 100%" type="del">删除</text-button>
                  </DropdownItem>
                </DropdownMenu>
              </template>
            </Dropdown>
          </div>
        </div>
        <div class="origin" v-if="item.orgi">by {{ item.orgi }}</div>
        <!-- <div class="type type-blue">{{ TYPE_NAME[item.aitype] }}</div> -->
        <!-- <div class="datetime">{{ item.creater }}·最近编辑{{ formatToDateTimse(item.updatetime || item.createtime) }}</div> -->
      </div>
    </div>
    <div class="no-content" v-else>
      <Icon type="ios-folder-open-outline" size="60" />
      <div class="tip">暂无数据~</div>
    </div>
  </div>

  <Page
    v-if="dataList"
    v-model="pageIndex"
    :page-size="pageSize"
    :total="total"
    show-elevator
    show-total
    @on-change="getList()"
  />

  <EditDialog ref="editRef" :parentId="parentId" @submit="editSuccess" />
</template>

<script setup>
import { ref, getCurrentInstance } from 'vue'
import { useRouter } from 'vue-router'
import { listPlugin, deletePlugin } from '@/api/modules/plugin'

import { formatToDateTime } from '@/util/dateUtil'

import EditDialog from './components/EditDialog.vue'

const props = defineProps({
  parentId: [String, Number]
})

const emit = defineEmits(['back'])

const { proxy } = getCurrentInstance()

// 分页
const pageIndex = ref(1)
const pageSize = 20
const total = ref(0)
const loading = ref(false)
// 项目列表
const dataList = ref([])
const searchText = ref('')
// 路由
const router = useRouter()

const title = ref('')
// 获取列表
const getList = async (reset) => {
  if (reset) {
    pageIndex.value = 1
  }
  loading.value = true
  try {
    const { data } = await listPlugin({
      query: {
        p: pageIndex.value,
        ps: pageSize
      },
      data: {
        name: searchText.value,
        parentid: props.parentId
      }
    })
    dataList.value = data.pluginInfoiList.content
    total.value = data.pluginInfoiList.totalElements
    title.value = data.infovo.name
  } finally {
    loading.value = false
  }
}
getList()

// 新增项目
const editRef = ref(null)
const addProject = () => {
  editRef.value?.initDialog()
}
const editSuccess = () => {
  // 重置
  getList(true)
}

const handleItemClick = (id) => {
  editRef.value?.initDialog(id)
}

// 删除项目
const handleRemove = (item, index) => {
  proxy.$Modal.confirm({
    mask: false,
    title: '警告',
    content: `<p>是否确认删除${item.name}?</p>`,
    okText: '确认',
    cancelText: '取消',
    onOk: async () => {
      await deletePlugin({
        data: { id: item.id }
      })
      proxy.$Message.success('确认移除')
      getList()
    }
  })
}

const stopEvent = () => {}
</script>

<style lang="scss" scoped>
.plugin-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #fff;
  padding: 10px;
  &__title {
    font-size: 18px;
    font-weight: 600;
    cursor: pointer;
    &:hover {
      opacity: 0.8;
    }
  }
}
.grid-context {
  height: calc(100vh - 190px);
  overflow-y: auto;
  background-color: #fff;
}
.plugin-content {
  background-color: #fff;
  padding: 10px;
  margin-top: 10px;
  // display: grid;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));
  grid-gap: 8px;
  max-height: 100%;
  overflow-y: auto;
}
.plugin-content-item {
  height: 140px;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid rgba(82, 100, 154, 0.13);
  cursor: pointer;
  &.checkbox {
    height: 160px;
  }
  .header {
    display: flex;
    justify-content: space-between;
    padding-bottom: 10px;
    height: 100px;
  }
  .icon {
    width: 60px;
    height: 60px;
    border-radius: 12px;
  }
  .title {
    font-size: 16px;
    margin-bottom: 10px;
  }
  .description {
    font-size: 13px;
    color: #999;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }
  .datetime {
    font-size: 12px;
    color: #999;
  }
  .type {
    width: 50px;
    text-align: center;
    padding: 2px;
    border-radius: 2px;
    font-size: 12px;
    background-color: rgba(87, 104, 161, 0.08);
    color: rgba(15, 21, 40, 0.82);
    &.type-blue {
      background-color: rgba(171, 181, 255, 0.3);
      color: rgb(81, 71, 255);
    }
  }
  .origin {
    font-size: 12px;
    color: #999;
    text-align: right;
  }
}

.check-project-btns {
  padding-top: 10px;
  /* text-align: right; */
  .btn {
    margin-right: 10px;
  }
}

.no-content {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  color: #666;
  background-color: #fff;
}

.ivu-page{
  background-color: #fff;
  text-align: right;
  margin-right: 24px;
}
</style>
